package server

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"robot/study/LiveRobot/define"
	"robot/study/LiveRobot/reptile"
	"strings"
)

func process(conn net.Conn) {
	var mm define.Message2
	defer func(conn net.Conn) {
		err := conn.Close()
		if err != nil {

		}
	}(conn)
	buf := make([]byte, 1000)
	n, err := conn.Read(buf[:])
	if err != nil {
		fmt.Println("获取数据出错了", err)
	}
	var s []byte
	for i, v := range buf[:n] {
		if v == 123 {
			s = buf[i:n]
			break
		}
	}
	fmt.Println("收到数据", string(s))
	err2 := json.Unmarshal(s, &mm)
	if err2 != nil {
		fmt.Println("unmarshal失败", err2)
	}
	fmt.Println(mm)
	_, err = conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n"))
	if err != nil {
		return
	}
	//--------------------------事件处理
	//if mm.MessageType == "group" {
	//	//专治xxx表情包
	//	if strings.Contains(mm.RawMessage, "[CQ:image,file") && reptile.Reb.Flag == true {
	//		reptile.Reb.U = &mm
	//		reptile.Reb.Work()
	//		return
	//	}
	//	//获取所有指令
	//	if mm.UserId != define.QQ && strings.Contains(mm.RawMessage, "[CQ:at,qq=1736979719]") {
	//		if strings.Contains(mm.RawMessage, "所有指令") {
	//			return
	//		}
	//		l := strings.Index(mm.RawMessage, "[CQ:at,qq=1736979719]")
	//		if mm.RawMessage[len(mm.RawMessage)-1:] == "]" {
	//			mm.RawMessage = mm.RawMessage[:l] + mm.RawMessage[l+21:]
	//		} else {
	//			mm.RawMessage = mm.RawMessage[:l] + mm.RawMessage[l+22:]
	//		}
	//		fmt.Println(mm.RawMessage)
	//		Handle(&mm) //处理信息
	//	}
	//} else {
	//	Handle(&mm)
	//}
	//RouterInit(&mm)
}

//socket链接qq号
func Sendmsg(m *define.Msg) {
	var b define.Body
	var bp define.Bodyprivate
	var url string
	var x []byte
	if m.Msg_type == "group" {
		url = "http://127.0.0.1:5700/send_group_msg"
		b = define.Body{
			Group_id: m.Number,
			Message:  m.Msg,
		}
		x, _ = json.Marshal(b)
	} else {
		url = "http://127.0.0.1:5700/send_private_msg"
		bp = define.Bodyprivate{
			User_id: m.Number,
			Message: m.Msg,
		}
		x, _ = json.Marshal(bp)
	}
	res, err := http.Post(url, "application/json;charset=utf-8", strings.NewReader(string(x)))
	if err != nil {
		fmt.Println("post发送失败", err)
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {

		}
	}(res.Body)
	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("读取响应失败")
	}
	fmt.Println("发送数据：", string(x))
	fmt.Println("响应数据：", string(content))

}

//Handle 处理消息
func Handle(u *define.Message2) {
	if strings.Contains(u.RawMessage, define.TRANSLATE) {
		sw := reptile.Sweet{U: u}
		sw.Work()
		return
	}
	if strings.Contains(u.RawMessage, define.HOT) {
		vi := reptile.Video{U: u}
		vi.Work()
		return
	}
	if strings.Contains(u.RawMessage, define.VIDEO) {
		reptile.SendVideo(u)
		return
	}
	if strings.Contains(u.RawMessage, define.REVIEWOFF) {
		reptile.Reb.Workoff()
		return
	}
	if strings.Contains(u.RawMessage, define.REVIEWOPEN) {
		reptile.Reb.Workopen()
		return
	}
	if strings.Contains(u.RawMessage, define.SETU) {
		ie := reptile.Image{U: u}
		ie.Work() //发送se图
		return
	} else {
		resa := reptile.Response{U: u}
		resa.Work()
	}
}
